package com.theobserver.theobserver;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wssxk on 7/11/17.
 */

public class SearchActivity extends Activity {


    ImageView imageview_search = null;
    EditText edittext_input = null;
    NewsListAdapter adapter = null;
    ListView lv = null;
    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        setContentView(R.layout.news_hot_search_page);

        View buttom_add_more = (View) findViewById(R.id.relativelayout_news_addmore);
        buttom_add_more.setVisibility(View.GONE);
        final SwipeRefreshLayout sl = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout_news);
        sl.setEnabled(false);

        TextView textview_return = (TextView) findViewById(R.id.textview_search_return);
        textview_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        lv = (ListView) findViewById(R.id.listview_news);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent news_detail_intent = new Intent(SearchActivity.this, NewsDetailActivity.class);
                //news_detail_intent.putExtras();
                news_detail_intent.putExtra("objectId",
                        ((NewsItemFrameLayout) view.findViewById(R.id.framelayout_news_item)).news.cur_news.getObjectId());
                startActivity(news_detail_intent);
            }
        });

        imageview_search = (ImageView) findViewById(R.id.imageview_search_pager_search);
        edittext_input = (EditText) findViewById(R.id.edittext_search_input);

        adapter = new NewsListAdapter(SearchActivity.this);
        lv.setAdapter(adapter);
        //sl.setRefreshing(true);
        //点击搜索
        imageview_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(edittext_input.getText().toString() != null
                        && edittext_input.getText().toString().equals("") != true)
                {
                    sl.setRefreshing(true);
                    News.getSearchNews(edittext_input.getText().toString(), new NewsCallBack() {
                        @Override
                        public void getNews(ArrayList<News> news, int state, AVException e) {
                            sl.setEnabled(false);
                            if(news != null && news.size() != 0)
                            {
                                adapter.data.clear();
                                adapter.addItems(news);
                                adapter.notifyDataSetChanged();
                            }
                            else
                            {
                                adapter.data.clear();
                                adapter.notifyDataSetChanged();
                                Toast.makeText(SearchActivity.this, "无搜索结果", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }
            }
        });
    }
}
